/// 全局状态
import type { Menu } from '@expresso/plus'
import { isEmpty } from 'lodash-es'
import { cache } from '@/helpers'

export default createPinia()
export * from '@expresso/plus/stores/index'
export * from './modules/login.store'

function installStore() {
  useLoadingStore.install()
  useMenusStore.store = useMenusStore()
  ExpressoPlusConfig.stores.menus = {
    menuTreeDataKey: 'permissionList',
    getButtonMap() {
      return cache.getLoginInfo()?.buttonMap ?? {}
    },
    getMenuTreeData() {
      return (
        (cache.getLoginInfo()?.permissionList ?? [])[0]
          ?.permissionList ?? []
      )
    },
    toMenu(data, menusData) {
      const menu = {
        key: `${data.id}`,
        label: data.permissionName,
        type: data.permissionType,
        position: data.position || 'right',
        url: data.permissionPath,
        children: isEmpty(data.permissionList) ? void 0 : [],
        icon: data.permissionIcon ? h(ExIcon, { type: data.permissionIcon }) : void 0,
        flag: data.permissionFlag,
        code: data.permissionKey,
        linked: data.openType == 2,
      } as Menu
      menusData.key2MenuMap[menu.key] = menu
      return menu
    },
  }
}

export { installStore }
